package org.windey.client;


/**
 * 
 * 
 *
 */
public class WindeyClientStreamThread extends Thread
{
	private WindeyClientThreadInterface wcti;
	private WindeyClientDataProvider wcdp;
	private WindeyServerConnection conn;
	
	/**
	 * 
	 * @param windeyClientDataProvider
	 * @param wcti
	 * @param conn
	 */
	public WindeyClientStreamThread(WindeyClientDataProvider windeyClientDataProvider, WindeyClientThreadInterface wcti, WindeyServerConnection conn)
	{
		this.conn = conn;
		this.wcti = wcti;
		this.wcdp = windeyClientDataProvider;
		//wcdp.setPriority(MAX_PRIORITY) ;
	}
	
	/**
	 * 
	 */
	public void run()
	{
		if (wcti.isStreaming() && !wcdp.isAlive())
		{
			wcdp.start();
		}
		while (wcti.isStreaming())
		{
			try
			{
				String data = wcdp.getNextDataString();
				if (!data.equalsIgnoreCase("0"))
				{
					conn.send(data);
					//System.out.println("we zijn aant zenden: " + wcdp.getBufferSize() ) ;
				}
				else
				{
					//WindeyClientStreamThread.sleep(100);
					//System.out.println("in den else") ;
				}
				while(wcdp.getBufferSize() < 4) {
					//System.out.println(wcdp.getBufferSize() + " is de buffersize") ;
					try {
						WindeyClientStreamThread.sleep(100);
						
					}
					catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
				}
				
			}
			catch (NullPointerException ex)
			{
		
			} 
			/*catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
		}
	}
}
